// pages/project_list_edit/project_list_edit.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType:'',
    projectId: 0,
    projectName: '',
    projectDesc: '',
    projectStatusDisabled: true, // 是否已完成
    projectManagerFlag: false, // 是否负责人
    constitution: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // pagetype [desc , constitution]
    let pageType = options.pageType;

    // 修改描述信息
    if  (pageType=='desc') {
      wx.setNavigationBarTitle({
        title: '项目描述',
      })

      this.setData({
        pageType:pageType,
        projectId: options.projectId,
        projectName: options.projectName,
        projectStatusDisabled:options.projectStatusDisabled=='true'?true:false,
        projectManagerFlag:options.projectManagerFlag=='true'?true:false,
        projectDesc: wx.getStorageSync('projectDesc'),
        userId: wx.getStorageSync('userId')
      })
      // 清除项目的缓存
      wx.removeStorageSync('projectDesc');
    }
    
    // 修改动态信息
    if (pageType=='constitution') {
      wx.setNavigationBarTitle({
        title: '项目动态',
      })

      this.setData({
        pageType:pageType,
        projectId: options.projectId,
        projectName: options.projectName,
        projectStatusDisabled:options.projectStatusDisabled=='true'?true:false,
        projectManagerFlag:options.projectManagerFlag=='true'?true:false,
        constitution: wx.getStorageSync('constitution'),
        userId: wx.getStorageSync('userId')
      })
      // 清除动态的缓存
      wx.removeStorageSync('constitution');
    }
  },

  formSubmit(e) {
    let param = e.detail.value;
    param.userId = this.data.userId;
    param.projectId = this.data.projectId;

    request.POST('/project/updateProjectDesc', param).then(res => {
      if (res.statusCode==200) {
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)
      } else {
        wx.showToast({
          title: '保存异常失败',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  formSubmit2(e) {
    let param = e.detail.value;
    param.userId = this.data.userId;
    param.projectId = this.data.projectId;

    request.POST('/project/updateConstitution', param).then(res => {
      if (res.statusCode==200) {
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)
      } else {
        wx.showToast({
          title: '保存异常失败',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '项目设置'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '项目设置'
    }
  },

})